#include <cs50.h>
#include <stdio.h>
#include <string.h>

bool linear_search(int n ,int numbers[], int size);
bool binary_search(int n ,int numbers[], int size);

int main(void)
{
    int numbers[] = { 1, 3, 10, 20, 39, 88};

    // 线性搜索
    string strings[] = {"battleship", "boot", "cannon", "iron", "thimble", "top hat"};

    string s = get_string("String: ");
    for (int i = 0; i < 6; i++)
    {
        if (strcmp(strings[i], s) == 0)
        {
            printf("Found\n");
            return 0;
        }
    }

    printf("Not found\n");
    return 1;
}
// 线性搜索函数部分实现
bool linear_search(int n ,int numbers[], int size){
    for(int i = 0;i< size;i++){
        if(numbers[i] == 0){
            return true;
        }
    }

    return false;

}

//二分搜索函数部分实现
bool binary_search(int n ,int numbers[], int size){
    int left = 0;
    int right = size - 1;

    while(left <= right){

        int mid = left + (right - left) / 2;
        if(numbers[mid] == n)
        {
            return true;
        }
        else if(numbers[mid] < n)
        {
            left = mid + 1;
        }else
        {
            right = mid - 1;
        }
    }

    return false;


}
